package com.anti.modular.bus.roomexpenses.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.commom.core.utils.StringUtils;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.roomexpenses.entity.RoomExpenses;
import com.anti.modular.bus.roomexpenses.enums.RoomExpensesExceptionEnum;
import com.anti.modular.bus.roomexpenses.param.RoomExpensesParam;
import com.anti.modular.bus.roomexpenses.result.LeaseExpensesResult;
import com.anti.modular.bus.roomexpenses.result.RoomExpensesResult;
import com.anti.modular.bus.roomexpenses.service.RoomExpensesService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 房间费用表service接口实现类
 *
 * @author anti
 * @date 2021-04-18 17:08:44
 */
@Service
public class RoomExpensesServiceImpl implements RoomExpensesService {
    @Resource
    DBUtils dbUtils;

    @Override
    public RoomExpenses getById(RoomExpensesParam roomExpensesParam){
        String sql="select * from bus_room_expenses where id =? ";
        return dbUtils.get(sql,RoomExpenses.class,roomExpensesParam.getId());
    }

    @Override
    public List<RoomExpensesResult> getByIds(List<Long> ids) {
        String sql="select a.*,b.id as house_bill_detail_id  from bus_room_expenses A,house_bill_detail B where A.id =B.rent_amount_category and a.status =0 and b.id in (:ids)";
        HashMap<Object, Object> param = CollectionUtil.newHashMap();
        param.put("ids",ids);
        List<RoomExpensesResult> roomExpenses = dbUtils.query(sql, RoomExpensesResult.class, param);
        return roomExpenses;
    }

    @Override
    public PageResult page(PageParam pageParam,RoomExpensesParam roomExpensesParam) {
        StringBuilder sb=new StringBuilder("select * from bus_room_expenses where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(roomExpensesParam)) {
            // 根据费用名称 查询
            if (ObjectUtil.isNotEmpty(roomExpensesParam.getName())) {
               sb.append(" and name = ?");
               param.add(roomExpensesParam.getName());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<RoomExpenses> list(RoomExpensesParam roomExpensesParam) {
        String sql="";
        return dbUtils.query(sql,RoomExpenses.class);
    }

    @Override
    public List<RoomExpensesResult> getExpensesListByRoomId(Long  roomId) {
        String sql="select * from bus_room_expenses where roomid =? and status =0";
        List<RoomExpensesResult> expensesResultList = dbUtils.query(sql, RoomExpensesResult.class, roomId);
        expensesResultList.forEach(item->{
            item.setKey(item.getCostid());
        });
        return expensesResultList;
    }

    @Override
    public List<LeaseExpensesResult> getExpensesListByLeaseId(Long leaseId) {
        String sql="select * from bus_lease_expenses where lease_id =? and status =0";

        List<LeaseExpensesResult> expensesResultList = dbUtils.query(sql, LeaseExpensesResult.class, leaseId);
        expensesResultList.forEach(item->{
            item.setKey(item.getCostid());
        });
        return expensesResultList;
    }

    @Override
    public List<RoomExpensesResult> getExpensesListByRoomId(Long roomId,String type) {
        StringBuilder sql=new StringBuilder("select * from bus_room_expenses where roomid =? and status =0");
        if (StringUtils.isNotBlank(type)){
            if (type.equals("1")){
                sql.append(" and ydzd =1");
            }else if (type.equals("2")){
                sql.append(" and ydzd =0");
            }
        }
        List<RoomExpensesResult> expensesResultList = dbUtils.query(sql.toString(), RoomExpensesResult.class, roomId);
        expensesResultList.forEach(item->{
            item.setKey(item.getCostid());
        });
        return expensesResultList;
    }

    @Override
    public void add(RoomExpensesParam roomExpensesParam) {
        RoomExpenses roomExpenses = new RoomExpenses();
        BeanUtil.copyProperties(roomExpensesParam, roomExpenses);
        dbUtils.insert(roomExpenses);
    }

    @Override
    public void addEntity(RoomExpenses roomExpenses) {
        dbUtils.insert(roomExpenses);
    }

    public void delete(RoomExpensesParam roomExpensesParam) {
        String sql="delete  from bus_room_expenses where id =? ";
        dbUtils.execSql(sql,roomExpensesParam.getId());
    }

    @Override
    public void deleteFyByRoomId(Long roomId) {
        String sql="update bus_room_expenses set update_time=?,update_user=?,status=? where roomid =? and status =0";
        dbUtils.execSql(sql,new Date(), LoginContextHolder.me().getSysLoginUserId(),2,roomId);
    }

    public void edit(RoomExpensesParam roomExpensesParam) {
        RoomExpenses roomExpenses = this.queryRoomExpenses(roomExpensesParam);
        BeanUtil.copyProperties(roomExpensesParam, roomExpenses);
        dbUtils.update(roomExpenses);
    }

    @Override
    public RoomExpenses detail(RoomExpensesParam roomExpensesParam) {
        return this.queryRoomExpenses(roomExpensesParam);
    }

    /**
     * 获取房间费用表
     *
     * @author anti
     * @date 2021-04-18 17:08:44
     */
    private RoomExpenses queryRoomExpenses(RoomExpensesParam roomExpensesParam) {
        RoomExpenses roomExpenses = this.getById(roomExpensesParam);
        if (ObjectUtil.isNull(roomExpenses)) {
            throw new BusinessException(RoomExpensesExceptionEnum.NOT_EXIST);
        }
        return roomExpenses;
    }


}
